package Servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import DBFunction.GroupManager;
import DBFunction.UserManager;
import DTO.User;
import Parser.StringtoMD5;

/**
 * Servlet implementation class ServerUserManager
 */
public class ServerUserManager extends HttpServlet {
	private static final long serialVersionUID = 1L;
	static Logger logger = Logger.getLogger(ServerAdminLogin.class);
    /**Thread searcher;
     * @see HttpServlet#HttpServlet()
     */
    public ServerUserManager() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("userAction");
		if(action.compareTo("add")==0){
			String phoneNumber = request.getParameter("phoneNumber");
			String passwords = StringtoMD5.MD5(request.getParameter("passwords"));
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String email = request.getParameter("email");
			String address = request.getParameter("address");
			String groupId="";
			try {
				groupId = GroupManager.getGroupId(request.getParameter("addUserGroupName"));
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			User inputUser = new User(phoneNumber,passwords,groupId,firstName,lastName,email,address);
			try {
				UserManager.insert(inputUser);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(action.compareTo("edit")==0){
			String oldId = request.getParameter("oldUserId");
			String phoneNumber = request.getParameter("phoneNumber");
			String passwords = StringtoMD5.MD5(request.getParameter("passwords"));
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String email = request.getParameter("email");
			String address = request.getParameter("address");
			String groupId = "";
			User inputUser = new User(phoneNumber,passwords,groupId,firstName,lastName,email,address);
			if(request.getParameter("passwords").isEmpty()){
				try {
					UserManager.update(inputUser,oldId);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			else {
				try {
					UserManager.updateWithNewPasswords(inputUser,oldId);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		if(action.compareTo("move")==0){
			String[] moveUserList = request.getParameterValues("memberSelect");
			if (moveUserList!=null){
				String oldGroupName = request.getParameter("oldGroupName");
				String newGroupName = request.getParameter("moveToGroupFrom"+oldGroupName);
				try {
					for (int x=0;x < moveUserList.length;x++){
						UserManager.changeGroup(moveUserList[x],newGroupName);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		if(action.compareTo("delete")==0){
			String[] moveUserList = request.getParameterValues("memberSelect");
			if (moveUserList!=null){
				try {
					for (int x=0;x < moveUserList.length;x++){
						UserManager.deleteById(moveUserList[x]);
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/ServerDisplay");
		   dispatcher.forward( request, response );
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
